/* eslint-disable array-bracket-spacing */
/* eslint-disable eqeqeq */
/* eslint-disable no-unused-vars */
/* eslint-disable arrow-parens */
const { Controller } = require('egg');

const priceCodeAndMeaning = [
  { code: '9', meaning: '商务座', str: 'swz' },
  { code: 'P', meaning: '特等座', str: 'tdz' },
  { code: 'M', meaning: '一等座', str: 'ydz' },
  { code: 'O', meaning: '二等座', str: 'edz' },
  { code: 'S', meaning: '二等包座', str: 'edbz' },
  { code: '6', meaning: '高级软卧', str: 'gjrw' },
  { code: 'A', meaning: '高级动卧', str: 'gjdw' },
  { code: '4', meaning: '软卧', str: 'rw' },
  { code: 'I', meaning: '一等卧', str: 'ydw' },
  { code: 'F', meaning: '动卧', str: 'dw' },
  { code: '3', meaning: '硬卧', str: 'yw' },
  { code: 'J', meaning: '二等卧', str: 'edw' },
  { code: '2', meaning: '软座', str: 'rz' },
  { code: '1', meaning: '硬座', str: 'yz' },
  { code: '8', meaning: '无座', str: 'wz' },
];




function makePrice(dto) {
  let priceObj = {};//返回价格的obj
  const price_code = dto.yp_info_new;//价格的哪个编码



  const priceCodeAndMeaning = [
    { code: '9', meaning: '商务座', str: 'swz' },
    { code: 'P', meaning: '特等座', str: 'tdz' },
    { code: 'M', meaning: '一等座', str: 'ydz' },
    { code: 'O', meaning: '二等座', str: 'edz' },
    { code: 'S', meaning: '二等包座', str: 'edbz' },
    { code: '6', meaning: '高级软卧', str: 'gjrw' },
    { code: 'A', meaning: '高级动卧', str: 'gjdw' },
    { code: '4', meaning: '软卧', str: 'rw' },
    { code: 'I', meaning: '一等卧', str: 'ydw' },
    { code: 'F', meaning: '动卧', str: 'dw' },
    { code: '3', meaning: '硬卧', str: 'yw' },
    { code: 'J', meaning: '二等卧', str: 'edw' },
    { code: '2', meaning: '软座', str: 'rz' },
    { code: '1', meaning: '硬座', str: 'yz' },
    { code: '8', meaning: '无座', str: 'wz' },
  ];
  function b5(c9) {
    if (c9 == '0') {
      return '';
    }
    const c8 = c9.toString().indexOf('.') == -1 ? c9 + '.0' : c9;
    return c8;
  }

  function aw(de) {
    let dg = '';
    const c8 = {};
    const dc = de.length / 7;
    for (let db = 0; db < dc; db++) {
      const df = de.substring(db * 7, db * 7 + 7);
      const da = df.substring(0, 1);
      const dd = df.substring(1, 2);
      const c9 = b5(Number(df.substring(2)) / 10);
      dg += da;
      c8[priceCodeAndMeaning.find((item) => item.code === da).meaning + ['未知铺位', '下铺', '中铺', '上铺'][dd]] = c9;
    }
    return c8;
  }

  const groupsCount = price_code.length / 10;
  for (let priceGroup = 0; priceGroup < groupsCount; priceGroup++) {
    const priceGroupStr = price_code.substring(
      10 * priceGroup,
      10 * (priceGroup + 1)
    );

    let priceCode = priceGroupStr.substring(0, 1);
    const price = priceGroupStr.substring(1, 6) / 10;
    const dd = priceGroupStr.substring(6, 10);
    if (dd > 3000) {
      priceCode = 8;
    }

    const priceName = priceCodeAndMeaning.find(
      (item) => item.code == priceCode).meaning;
    priceObj[priceName] = price;
  }


  if (dto.bed_level_info) {
    priceObj = Object.assign({}, priceObj, aw(dto.bed_level_info));
  }

  return priceObj;
}











function cM(db, dd) {
  const da = [];
  for (let c9 = 0; c9 < db.length; c9++) {
    const de = {};
    const c8 = db[c9].split('|');
    // de.secretStr = c8[0];
    // de.buttonTextInfo = c8[1];
    const dc = {};
    // dc.train_no = c8[2];
    dc.station_train_code = c8[3];
    // dc.start_station_telecode = c8[4];
    // dc.end_station_telecode = c8[5];
    // dc.from_station_telecode = c8[6];
    // dc.to_station_telecode = c8[7];
    dc.start_time = c8[8];
    dc.arrive_time = c8[9];
    // dc.lishi = c8[10];
    dc.canWebBuy = c8[11];
    // dc.yp_info = c8[12];
    dc.start_train_date = c8[13];
    // dc.train_seat_feature = c8[14];
    // dc.location_code = c8[15];
    // dc.from_station_no = c8[16];
    // dc.to_station_no = c8[17];
    // dc.is_support_card = c8[18];
    // dc.controlled_train_flag = c8[19];
    // dc.gg_num = c8[20] ? c8[20] : '--';
    // dc.gr_num = c8[21] ? c8[21] : '--';
    // dc.qt_num = c8[22] ? c8[22] : '--';
    // dc.rw_num = c8[23] ? c8[23] : '--';
    // dc.rz_num = c8[24] ? c8[24] : '--';
    // dc.tz_num = c8[25] ? c8[25] : '--';
    // dc.wz_num = c8[26] ? c8[26] : '--';
    // dc.yb_num = c8[27] ? c8[27] : '--';
    // dc.yw_num = c8[28] ? c8[28] : '--';
    // dc.yz_num = c8[29] ? c8[29] : '--';
    // dc.ze_num = c8[30] ? c8[30] : '--';
    // dc.zy_num = c8[31] ? c8[31] : '--';
    // dc.swz_num = c8[32] ? c8[32] : '--';
    // dc.srrb_num = c8[33] ? c8[33] : '--';
    // dc.yp_ex = c8[34];
    // dc.seat_types = c8[35];
    // dc.exchange_train_flag = c8[36];
    // dc.houbu_train_flag = c8[37];
    // dc.houbu_seat_limit = c8[38];
    dc.yp_info_new = c8[39];
    // dc.dw_flag = c8[46];
    // dc.stopcheckTime = c8[48];
    // dc.country_flag = c8[49];
    // dc.local_arrive_time = c8[50];
    // dc.local_start_time = c8[51];
    dc.bed_level_info = c8[53];
    // dc.seat_discount_info = c8[54];
    // dc.sale_time = c8[55];
    dc.from_station_name = dd[c8[6]];
    dc.to_station_name = dd[c8[7]];
    // console.log(dc);
    de.queryLeftNewDTO = dc;

    de.priceObj = makePrice(dc);
    da.push({ priceObj: makePrice(dc), ...dc });
    // console.log(da);
  }
  return da;
}

class TrainController extends Controller {
  async priceSearch() {
    const { ctx } = this;
    const { train_date, from_station, to_station } = ctx.query;
    const url = `https://kyfw.12306.cn/otn/leftTicket/queryE?leftTicketDTO.train_date=BJP&leftTicketDTO.from_station=TJP&leftTicketDTO.to_station=2024-01-25&purpose_codes=ADULT`;
    const result = await this.ctx.curl(url, {
      // 自动解析 JSON response
      dataType: 'json',
      headers: {
        cookie: ctx.app.session,
      },
      timeout: 3000,
    });
    const lista = cM(result.data.data.result, result.data.data.map);
    ctx.body = lista;
  }

  async stationList() {
    const { ctx } = this;
    const result = await this.ctx.curl(
      'https://www.12306.cn/index/script/core/common/station_name_new_v10037.js',
      {
        // 自动解析 JSON response
        dataType: 'text',
        headers: {
          'Content-Type': 'application/json',
          // Host: 'www.ftms.com.cn',
        },
        timeout: 3000,
      }
    );

    let reusltstr = result.data;
    reusltstr = reusltstr.split("='")[1];
    reusltstr = reusltstr.split("';")[0];
    const stations = reusltstr.split('@');
    const stationList = [];
    stations.forEach((station) => {
      if (station.indexOf('|') !== -1) {
        stationList.push({
          zhname: station.split('|')[1],
          code: station.split('|')[2],
          engname: station.split('|')[3],
          litename: station.split('|')[4],
        });
      }
    });
    ctx.body = stationList;
  }
}

module.exports = TrainController;
